package com.taotao.content.service.impl;

import com.sun.tools.internal.ws.wsdl.document.jaxws.Exception;
import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.content.jedis.JedisClient;
import com.taotao.content.jedis.JedisClientPool;
import com.taotao.content.service.ContentService;
import com.taotao.content.service.pojo.Ad1Node;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service("contentServiceImpl")
public class ContentServiceImpl implements ContentService {

    @Value("${REDIS_KEY}")
    private String REDIS_KEY;
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Autowired
    private TbContentMapper tbContentMapper;

    @Override
    public List<EasyUITreeNode> commonTree(Long parentId) {

        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> tbContentCategoryList = tbContentCategoryMapper.selectByExample(example);
        List<EasyUITreeNode> list = new ArrayList<>();
        for (TbContentCategory tbContentCategory : tbContentCategoryList) {
            EasyUITreeNode easyUITreeNode = new EasyUITreeNode();
            easyUITreeNode.setId(tbContentCategory.getId());
            easyUITreeNode.setText(tbContentCategory.getName());
            easyUITreeNode.setState(tbContentCategory.getIsParent()?"closed":"open");
            list.add(easyUITreeNode);
        }

        return list;
    }

    @Override
    public TaotaoResult createCategory(Long parentId, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setId(IDUtils.genItemId());
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setName(name);
        tbContentCategory.setStatus(1);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setIsParent(false);
        tbContentCategoryMapper.insert(tbContentCategory);
        TbContentCategory parentCategory = tbContentCategoryMapper.selectByPrimaryKey(parentId);
        if (!parentCategory.getIsParent()){
            parentCategory.setIsParent(true);
            tbContentCategoryMapper.updateByPrimaryKey(parentCategory);
        }

        return TaotaoResult.ok(tbContentCategory);
    }

    @Override
    public TaotaoResult updateCategory(Long id, String name) {
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(id);
        tbContentCategory.setName(name);
        tbContentCategoryMapper.updateByPrimaryKey(tbContentCategory);
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteCategory(Long id) {
        boolean b = true;
        deleteSon(id);
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult contentSave(TbContent content) {
        content.setCreated(new Date());
        content.setUpdated(new Date());
        tbContentMapper.insertSelective(content);
        return TaotaoResult.ok();
    }

    @Override
    public List<TbContent> getContentList(Long cid) {
        JedisClient jedisClient = new JedisClientPool();
        try {
            //先查询缓存,缓存有的直接返回
            String json = jedisClient.hget(REDIS_KEY, cid + "");
            if (StringUtils.isNotBlank(json)){
                List<TbContent> contents = JsonUtils.jsonToList(json, TbContent.class);
                return contents;
            }

        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }


        //根据cid查询内容列表
        TbContentExample example = new TbContentExample();
        //设置查询条件
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(cid);
        //执行查询
        List<TbContent> list = tbContentMapper.selectByExample(example);

        try {
           jedisClient.hset(REDIS_KEY,cid+"", JsonUtils.objectToJson(list));
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return list;


    }

    private void deleteSon(Long id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(example);
        if (list.size()!=0){
            for (TbContentCategory tbContentCategory : list) {
                deleteSon(tbContentCategory.getId());
            }
        }
        tbContentCategoryMapper.deleteByPrimaryKey(id);

    }
}
